let xlsx = require('node-xlsx');
const fs = require('fs')
const path = require('path')

// 解析得到文档中的所有 sheet
let sheets = xlsx.parse(fs.readFileSync(path.join(__dirname, '行业表_20210823.xlsx')));
// 遍历 sheet
sheets.forEach((sheet) => {
	let map = {};
	let rows = [];
	// 遍历xlsx每行内容
	let list = sheet['data']
	list.shift()
	for (let row of sheet['data']) {
		let industry = row[2]
		let item = {
			id: row[0],
			label: row[3],
			mcccode: row[4],
			value: row[4]
		}
		if (!map[industry]) {
			map[industry] = []
		}
		map[industry].push(item)
	}
	Object.entries(map).forEach(([k, v], i) => {
		rows.push({
			value: i + 1,
			label: k,
			children: v
		})
	})
	// console.log(map);
	fs.writeFile(`${sheet.name}.json`, JSON.stringify(rows), res => {
		console.log('write success');
	})
});
